package com.user.ui;

import com.common.result.ResponseResultAdvice;
import com.user.application.command.cmd.UserCommand;
import com.user.application.command.service.UserCommandService;
import com.user.application.query.dto.UserDto;
import com.user.application.query.service.UserQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.security.filter.AbstractTokenAuthenticationFilter.NO_TOKEN_AUTH_REQUEST_PREFIX;

/**
 * @author 大忽悠
 * @create 2022/9/15 11:09
 */
@Validated
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags = "用户模块")
public class UserController {
   private final UserCommandService userCommandService;
   private final UserQueryService userQueryService;

    @ApiOperation("用户注册,注册成功,返回新用户ID")
    @PostMapping("/register")
    public Integer userRegistration(@Valid UserCommand userCommand) throws IOException {
        return userCommandService.registerUser(userCommand);
    }

    @ApiOperation("用户登录")
    @PostMapping(NO_TOKEN_AUTH_REQUEST_PREFIX+"/login")
    public String userLogin(@RequestParam("username") String username, @RequestParam("password") String password){
       return userCommandService.login(username,password);
    }

    @ApiOperation("获取所有用户")
    @GetMapping("/getAllUsers")
    public List<UserDto> getAllUsers(){
        return userQueryService.getAllUsers();
    }
}
